package com.ruifu.pls.service;

import com.ruifu.pls.entity.DocOrderDetails;
import com.ruifu.pls.entity.DocOrderHeader;
import com.ruifu.pls.model.GLCallInputParam;
import com.ruifu.pls.model.odoReturnAdd.GLinOrder;
import com.ruifu.pls.model.odoReverseAddConform.GLinReverseOrder;
import com.ruifu.pls.repository.GDocOrderDetailsRepository;
import com.ruifu.pls.repository.GDocOrderHeaderRepository;
import com.ruifu.pls.utils.HttpUtils;
import com.ruifu.pls.utils.SighUtil;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.io.IOException;
import java.util.List;

/**
 * ********************************************************
 * * @desc  ：
 * * @author  ${user}
 * * @date    ${date}
 * * @version v1.0
 * * ${tags}
 * *******************************************************
 */
@Service
public class DocOrderHeaderAddService {
    private org.slf4j.Logger logger = LoggerFactory.getLogger(DocOrderHeaderService.class);
    @Autowired
    GDocOrderHeaderRepository docOrderHeaderRepository;
    @Autowired
    GDocOrderDetailsRepository docOrderDetailsRepository;
    //出库单新增接口
    @Transactional
    public  void postDocOrderHeaderToGlinAdd() {
        DocOrderHeader docOrderHeader = docOrderHeaderRepository.findByDocOrderHeaderForReverseAdd();

        if(docOrderHeader!=null) {
            String orderno = docOrderHeader.getOrderno();
            String ordertype = docOrderHeader.getOrdertype();
            List<DocOrderDetails> glDocOrderDetailsList1 = docOrderDetailsRepository.findDocOrderDetailsByAdd(orderno);
            GLinOrder gLinOrder = docOrderHeader.gLinOrder;
            gLinOrder.getInfo= docOrderHeader.gLinGetInfo;
            for (int i = 0; i < glDocOrderDetailsList1.size(); i++) {
                gLinOrder.orderLines.add(glDocOrderDetailsList1.get(i).gLinOrderLinesno);
            }

            GLinOrder glReturnAsnInputRaram1 = gLinOrder;

            GLCallInputParam glCallInputParam1 = new GLCallInputParam();
            glCallInputParam1.setData(gLinOrder);
            glCallInputParam1.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam1)));
            glCallInputParam1.setMethod("g2matrix.outboundorder.outBoundInfor");

            logger.info(glCallInputParam1.toString());
            try {
                String responsse =HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam1.toString());                    //System.out.println(responsse);
                logger.info(responsse);
                if (responsse.contains("\"code\":0")) {
                    docOrderHeaderRepository.findByReturnUpdate(orderno);
                } else {
                    docOrderHeaderRepository.findByReturnUpdateEE(orderno);
                    logger.info(responsse);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //销售单新增接口
    @Transactional
    public  void postDocOrderHeaderToGlinAddComform() {
        DocOrderHeader docOrderHeader = docOrderHeaderRepository.findByDocOrderHeaderAddConform();

        //String ordertype = docOrderHeader.getOrdertype();
        if(docOrderHeader!=null) {
            String orderno = docOrderHeader.getOrderno();
           List<DocOrderDetails> glDocOrderDetailsList1 = docOrderDetailsRepository.findDocOrderDetailsByAdd(orderno);
            GLinReverseOrder gLinOrder = docOrderHeader.gLinReverseOrder;
            gLinOrder.getInfo=docOrderHeader.gLinReverseGetInfo;
           // gLinOrder.invoices.add(docOrderHeader.gLinReverseInvoies);
            for (int i = 0; i < glDocOrderDetailsList1.size(); i++) {
                gLinOrder.cargo.add(glDocOrderDetailsList1.get(i).gLinReverseCargo);
            }

            GLinReverseOrder glReturnAsnInputRaram1 = gLinOrder;

            GLCallInputParam glCallInputParam1 = new GLCallInputParam();
            glCallInputParam1.setData(gLinOrder);
            glCallInputParam1.setMd5Sigh(SighUtil.getMD5(SighUtil.signParam(glCallInputParam1)));
            glCallInputParam1.setMethod("g2matrix.saleorder.syncInsertOrders");

            logger.info(glCallInputParam1.toString());
            try {
                String responsse =HttpUtils.doPost("http://demo.api.g2link.cn:8081/", "router", glCallInputParam1.toString());
                logger.info(responsse);
                if (responsse.contains("\"code\":0")) {
                    docOrderHeaderRepository.findByReturnUpdate(orderno);
                } else {
                    docOrderHeaderRepository.findByReturnUpdateEE(orderno);
                    logger.info(responsse);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
